package com.db;

import com.db.bankapp.domain.bank.*;
import com.db.bankapp.exception.NotEnoughFundsException;
import com.db.bankapp.exception.OverDraftLimitExceedException;
import com.db.bankapp.service.bank.BankService;

/**
 * Main class
 * Created by Student on 18.08.2014.
 */
public class BankApplication {
    private BankApplication() {}

    public static void main(String[] args) {
        Bank bank = new Bank();
        for (int i = 0; i < 2; i++) {
            BankService.addClient(bank, ClientBuilder.newClient("Eve").female().withAccount(
                    Bank.createCheckingAccount(10, 15)).build());
        }
        for (int i = 0; i < 2; i++) {
            BankService.addClient(bank, ClientBuilder.newClient("John").male().withAccount(
                    Bank.createSavingAccount(11)).build());
        }
        try {
            modifyBank(bank, 2, -16);
            modifyBank(bank, 3, -20);
        } catch(OverDraftLimitExceedException e) {
            e.printStackTrace();
            System.out.println("Not enough money during operation with " + e.getAmount() +"$." + " Balance: " + e.getBalance() + ", Overdraft: " + e.getOverdraft());
        } catch (NotEnoughFundsException e) {
            e.printStackTrace();
            System.out.println("Not enough money during operation with " + e.getAmount() + "$.");
        }
      //  printBalance(bank);
      //  BankService.printMaximumAmountToWithdraw(bank);
    }

    public static void printBalance(Bank bank) {
        for (Client client : bank.getClients()) {
            for (Account account : client.getAccounts()) {
                System.out.println(client.getClientSalutation() + " has " + account.getBalance() + "$");
            }
        }
    }

    public static void modifyBank(Bank bank, int clientId, int money) throws NotEnoughFundsException{
        Client client = bank.getClient(clientId);
        if (money > 0) {
            Account account = client.getAccount(1);
            account.deposit(money);
        } else if (money < 0) {
            Account account = client.getAccount(1);
            account.withdraw(-money);
        }
    }

}
